package com.glory.good_dark.adapter;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.good_dark.R;
import com.glory.good_dark.activity.ShowCheckActivity;
import com.glory.good_dark.application.App;
import com.glory.good_dark.entity.CheckTable;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;

import java.util.List;

/**
 * FileName: CheckTableListAdapter
 * Author: XJH
 * Date: 2020/4/8 17:55
 * Description:
 * History:
 */
public class CheckTableListAdapter extends BaseQuickAdapter<CheckTable, BaseViewHolder> {
    private OnDeleteClickListener listener;
    public CheckTableListAdapter(@Nullable List<CheckTable> data) {
        super(R.layout.item_check_list,data);
    }

    @Override
    protected void convert(BaseViewHolder helper, CheckTable item) {
        CheckBox cbCheck = helper.getView(R.id.cbCheck);
        cbCheck.setVisibility(View.VISIBLE);
        ImageView ivExecute = helper.getView(R.id.ivExecute);
        ImageView ivDelete = helper.getView(R.id.ivDelete);
        cbCheck.setChecked(item.isChecked());
        helper.setGone(R.id.tvExecute,false);
        helper.setGone(R.id.llExecuteLayout,true);
        helper.setText(R.id.tvCheckName,item.getCHECK_NAME());
        helper.setText(R.id.tvState,item.getCHECK_STATE());
        helper.setText(R.id.tvPMBeginTime,item.getPLAN_CHECK_STIME());
        helper.setText(R.id.tvPMEndTime,item.getPLAN_CHECK_ETIME());
        helper.setText(R.id.tvTimePM,item.getSTART_CHECK_TIME());
        helper.setText(R.id.tvStepTable,item.getCHECK_NAME_CONFIRM());
//        if (!item.getCHECK_STATE().equals("执行中")){
//            ivExecute.setEnabled(cbCheck.isChecked());
//        }else {
//            ivExecute.setEnabled(false);
//        }
        ivExecute.setEnabled(cbCheck.isChecked());
        ivDelete.setEnabled(cbCheck.isChecked());
        cbCheck.setTag(helper.getAdapterPosition());
        cbCheck.setOnClickListener(v -> {
//            if (!item.getCHECK_STATE().equals("执行中")){
//                ivExecute.setEnabled(cbCheck.isChecked());
//            }
            ivDelete.setEnabled(cbCheck.isChecked());
            item.setChecked(cbCheck.isChecked());
            ivExecute.setEnabled(cbCheck.isChecked());
        });

        if (item.getCHECK_STATE().equals("执行中")){
            ivExecute.setBackgroundColor(App.app.getResources().getColor(R.color.free_ui_deep_green));
            ivDelete.setVisibility(View.GONE);
        }

        ivDelete.setOnClickListener(v -> {
            if (listener != null){
                listener.onDelete(v,item.getID(),helper.getAdapterPosition());
            }
        });

        ivExecute.setOnClickListener(v -> {
            Bundle bundle = new Bundle();
            bundle.putString("GUID",item.getCHECK_ID());
            bundle.putString("tableName",item.getCHECK_NAME());
            bundle.putBoolean("checkState",item.getCHECK_STATE().equals("执行结束"));
            FreeApi_Utils.skipWithExtraIntent("header",bundle, mContext, ShowCheckActivity.class);
        });

    }

    public void setDeleteClickListener(OnDeleteClickListener listener){
        this.listener = listener;
    }


    public interface OnDeleteClickListener{
        void onDelete(View v,String s,int position);
    }
}
